<?php
class Person_Model extends CI_Model {
	public function __construct(){
		parent:: __construct();
	}

	public function get_name_country($countryId){
		$sql = $this->db->select('country')->get_where('regions', array('id' =>$countryId));
		$row = $sql->row();
		return $row->country;	
	}

	public function get_name_state($countryId){
		$sql = $this->db->select('name')->get_where('subregions', array('id' =>$countryId));
		$row = $sql->row();
		return $row->name;	
	}

	public function get_photo_person($personId) {
		$sql = $this->db->select('photo')->get_where('photo_person', array('person_id' => $personId))->row();
		if($sql->photo !== null) {
			return $sql->photo;
		} else {
			return null;
		}
	}

	public function get_person($id){
		$sql = $this->db->get_where('person', array('id' => $id))->row_array();
		return $sql;
	}

	public function get_name_person($id) {
		$sql = $this->db->select('id, name')->get_where('person', array('id' => $id))->row();
		return $sql->name;
	}

	public function delete($id) {
		//Delete all videos belong to this person
		$videos = $this->db->select('id')->get_where('person_video', array('personId' => $id))->result();
		if(count($videos)) {
			foreach($videos as $vd) {
				$this->db->delete('like_video', array('videoId' => $vd->id));
			}
		}

		$this->db->delete('person_video', array('personId' => $id));

		//Delete all photo of person from photo_person
		$getPhotos = $this->db->get_where('photo_person', array('person_id' => $id))->result_array();
		if(count($getPhotos)) {
			foreach($getPhotos as $photo) {
				if($photo['photo'] !== null) @unlink($photo['photo']);
			}
		}
		$this->db->delete('photo_person', array('person_id' => $id));

		//Delete all candle from person_candle
		$this->db->delete('person_candle', array('personId' => $id));

		//Delete all comment of person
		$this->db->delete('comment', array('personId' => $id));

		$getPhotoCover = $this->db->select('photo_cover')->get_where('person', array('id' => $id))->row_array();
		if($getPhotoCover['photo_cover'] !== null) @unlink($getPhotoCover['photo_cover']);
		$this->db->delete('person', array('id' => $id));
	}

	public function delete_pic($id) {
		$getPhotos = $this->db->select('photo')->get_where('photo_person', array('id' => $id))->row_array();
		if($getPhotos['photo'] !== null) @unlink($getPhotos['photo']);
		if($this->db->delete('photo_person', array('id' => $id))) {
			return TRUE;
		} else {
			return FALSE;
		}
	}
}